package cn._2vin.user.bingxuexiehuione.controller;

import java.io.IOException;
import java.io.Writer;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.weixin4j.WeixinException;



import cn._2vin.user.bean.ImgUrl;
import cn._2vin.user.bean.LianxiFood;
import cn._2vin.user.bingxuexiehuione.persistence.HomePageMeiShiMapper;
import cn._2vin.user.bingxuexiehuione.service.HomePageMeiShiService;
import cn._2vin.user.bingxuexiehuione.service.ImgUrlService;
import cn._2vin.user.utils.LocationUtils;

/*
 * 微信小程序首页
 */

@Controller
@RequestMapping("/user/bingxuexiehuitwo/*")
public class HomePageMeiShi {

	@Autowired
	private HomePageMeiShiMapper homePageMeiShiMapper;
	@Autowired
	private HomePageMeiShiService homePageMeiShiService;
	@Autowired
	private ImgUrlService imgUrlService;

	/*
	 * 小程序首页
	 */
	@RequestMapping("houtai.do")
	public String houtai(String code, HttpServletRequest request,
			HttpServletResponse response, Model model) throws WeixinException,
			IOException {

		System.out.println("来自客户端的消息:");
		response.setContentType("text/html;charset=utf-8");
		/* 设置响应头允许ajax跨域访问 */
		response.setHeader("Access-Control-Allow-Origin", "*");
		/* 星号表示所有的异域请求都可以接受， */
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		// WeixinUtils weixinUtils=new WeixinUtils();
		// String openid=weixinUtils.login(code, request);
		List<Object> list=new ArrayList<Object>();
		List<Object> list2=new ArrayList<Object>();
		try {
			int userclassid=1;
			for (int i = 0; i<7; i++) {
				userclassid=i+1;
				String userclassids=String.valueOf(userclassid);
				List<LianxiFood> lianxiFoods = homePageMeiShiService
						.queryPrimaryKey(userclassids);
				//String lianxifooduser=lianxiFoods.toString();
				list2.add(lianxiFoods);
			}
			String listString=list.toString();
			model.addAttribute("list2", list2);

			JSONArray json = JSONArray.fromObject(list2);

			Writer out = response.getWriter();

			out.write(json.toString());

			out.flush();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;

	}

	/*
	 * 小程序首页分类bane
	 */
	@RequestMapping("classification.do")
	public String classification(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("来自客户端的消息:");
		response.setContentType("text/html;charset=utf-8");
		/* 设置响应头允许ajax跨域访问 */
		response.setHeader("Access-Control-Allow-Origin", "*");
		/* 星号表示所有的异域请求都可以接受， */
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		try {
			String name = request.getParameter("name");

			List<LianxiFood> lianxiFoods = homePageMeiShiService
					.selectlistlianxifood(name);
			model.addAttribute("lianxiFoods", lianxiFoods);

			JSONArray json = JSONArray.fromObject(lianxiFoods);

			Writer out = response.getWriter();
			out.write(json.toString());

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * 小程序首页分类bane
	 */
	@RequestMapping("classificationtwo.do")
	public String classificationtwo(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("来自客户端的消息:");
		response.setContentType("text/html;charset=utf-8");
		/* 设置响应头允许ajax跨域访问 */
		response.setHeader("Access-Control-Allow-Origin", "*");
		/* 星号表示所有的异域请求都可以接受， */
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		try {
			String id = request.getParameter("id");

			List<LianxiFood> lianxiFoods = homePageMeiShiService
					.queryPrimaryKey(id);
			model.addAttribute("lianxiFoods", lianxiFoods);

			JSONArray json = JSONArray.fromObject(lianxiFoods);

			Writer out = response.getWriter();
			out.write(json.toString());

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * 小程序首页查询即其他位置的查询
	 */
	@RequestMapping("search.do")
	public String search(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("来自客户端的消息:");
		response.setContentType("text/html;charset=utf-8");
		/* 设置响应头允许ajax跨域访问 */
		response.setHeader("Access-Control-Allow-Origin", "*");
		/* 星号表示所有的异域请求都可以接受， */
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		try {
			// String name=request.getParameter("name");
			// String keyword=new String(name.getBytes("ISO-8859-1"),"UTF-8");
			String keyword = request.getParameter("name");

//			username=new String(username.getBytes("ISO-8859-1"),("utf-8"));
			keyword = java.net.URLDecoder.decode(keyword, "UTF-8");
			// keyword="海鲜";
			LianxiFood lianxiFood = new LianxiFood();
			lianxiFood.setFoodname(keyword);
			lianxiFood.setUsername(keyword);
			List<LianxiFood> list = homePageMeiShiService
					.listlianxifood(keyword);
			model.addAttribute("list", list);

			JSONArray json = JSONArray.fromObject(list);

			Writer out = response.getWriter();
			out.write(json.toString());

			out.flush();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return null;
	}

	/*
	 * 小程序首页进入视频详情页面，和其他链接进入详情页面
	 */
	@RequestMapping("Videodetails.do")
	public String Videodetails(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		/*
		 * 通过店铺的id进行跳转到视频详情页面 userid 9张图片加一个视频
		 */
		System.out.println("来自客户端的消息:");
		
		response.setContentType("text/html;charset=utf-8");
		/* 设置响应头允许ajax跨域访问 */
		response.setHeader("Access-Control-Allow-Origin", "*");
		/* 星号表示所有的异域请求都可以接受， */
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		try {
			String username = request.getParameter("username");
			username = java.net.URLDecoder.decode(username, "UTF-8");
			String id = request.getParameter("id");
			// keyword="海鲜";
			List<LianxiFood> lianxiFood=homePageMeiShiService.selectusername(username);
			model.addAttribute("lianxiFood", lianxiFood);
//			JSONObject json=new JSONObject();
//			json.accumulate("lianxiFood", lianxiFood);
             JSONArray   json = JSONArray.fromObject(lianxiFood);
			
			Writer out = response.getWriter();
			out.write(json.toString());
			out.flush();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return null;
	}

	/*
	 * 得到视频地址
	 */
	@RequestMapping("VideoUrl.do")
	public String VideoUrl(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		/*
		 * 通过店铺的id进行跳转到视频详情页面 userid 9张图片加一个视频
		 */
		System.out.println("来自客户端的消息:");
		
		response.setContentType("text/html;charset=utf-8");
		/* 设置响应头允许ajax跨域访问 */
		response.setHeader("Access-Control-Allow-Origin", "*");
		/* 星号表示所有的异域请求都可以接受， */
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		try {
			String username = request.getParameter("username");
			username = java.net.URLDecoder.decode(username, "UTF-8");
			String id = request.getParameter("id");
			// keyword="海鲜";
			List<LianxiFood> lianxiFood=homePageMeiShiService.selectusername(username);
			model.addAttribute("lianxiFood", lianxiFood);
//			JSONObject json=new JSONObject();
//			json.accumulate("lianxiFood", lianxiFood);
             JSONArray   json = JSONArray.fromObject(lianxiFood);
			
			Writer out = response.getWriter();
			out.write(json.toString());
			out.flush();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return null;
	}
	
	
	
	@RequestMapping("gainlatlogt.do")
	public String gainlatlogt(HttpServletRequest request,
			HttpServletResponse response) {
		/*
		 * 通过店铺的id进行跳转到视频详情页面 userid 9张图片加一个视频
		 */
		System.out.println("来自客户端的消息:");
		response.setContentType("text/html;charset=utf-8");
		/* 设置响应头允许ajax跨域访问 */
		response.setHeader("Access-Control-Allow-Origin", "*");
		/* 星号表示所有的异域请求都可以接受， */
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		try {
			String userlongitude = request.getParameter("longitude");
			double a = new Double(userlongitude);
			String userlatitude = request.getParameter("latitude");
			double b = new Double(userlatitude);
			List<LianxiFood> lianxiFoods = homePageMeiShiMapper
					.selectLianxiFood();
			String disString=null;
			List<String> list=new ArrayList<String>();
			for (int i = 0; i < lianxiFoods.size(); i++) {
				String longitude = lianxiFoods.get(i).getLongitude();
				double longit = new Double(longitude);
				String latitude = lianxiFoods.get(i).getLatitude();
				double lat = new Double(latitude);
				double distance = LocationUtils.getDistance(longit, lat, a, b);
				if (distance>=1000) {
					double dis=distance/1000;
					disString=String.format("%.2f",dis)+"km";
//					 disString=dis+"km";
				}else {
					double dis=distance;
					disString=String.format("%.2f",dis);
//					 disString=dis+"m";
				}
				list.add(disString);
			}
			JSONArray json=JSONArray.fromObject(list);
			Writer out = response.getWriter();
			out.write(json.toString());
			
			out.flush();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return null;
	}
	
	@RequestMapping("gainlatlogttwo.do")
	public String gainlatlogttwo(HttpServletRequest request,
			HttpServletResponse response) {
		/*
		 * 通过店铺的id进行跳转到视频详情页面 userid 9张图片加一个视频
		 */
		System.out.println("来自客户端的消息:");
		response.setContentType("text/html;charset=utf-8");
		/* 设置响应头允许ajax跨域访问 */
		response.setHeader("Access-Control-Allow-Origin", "*");
		/* 星号表示所有的异域请求都可以接受， */
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		try {
			String id=request.getParameter("id");
			String userlongitude = request.getParameter("longitude");
			double a = new Double(userlongitude);
			String userlatitude = request.getParameter("latitude");
			double b = new Double(userlatitude);
			LianxiFood lianxiFoods = homePageMeiShiMapper
					.queryLianxifood(id);
			String disString=null;
			List<String> list=new ArrayList<String>();
				String longitude = lianxiFoods.getLongitude();
				double longit = new Double(longitude);
				String latitude = lianxiFoods.getLatitude();
				double lat = new Double(latitude);
				double distance = LocationUtils.getDistance(longit, lat, a, b);
				if (distance>=1000) {
					double dis=distance/1000;
					disString=String.format("%.2f",dis)+"km";
//					 disString=dis+"km";
				}else {
					double dis=distance;
					disString=String.format("%.2f",dis);
//					 disString=dis+"m";
				}
				list.add(disString);
			
			JSONArray json=JSONArray.fromObject(list);
			Writer out = response.getWriter();
			out.write(json.toString());
			
			out.flush();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return null;
	}
}